﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunboxsoft.ASMS.Server.Domain.Calculation
{
    public static class MaintanceBillCalculation
    {
        /// <summary>
        /// 计算维修单金额
        /// </summary>
        /// <param name="serviceItemArray"></param>
        /// <param name="materialArray"></param>
        /// <param name="payItemArray"></param>
        /// <returns></returns>
        public static decimal Calc(Entity.EMaintenanceBillServiceItem[] serviceItemArray,
            Entity.EMaintenanceBillMaterial[] materialArray,
            Entity.EMaintenanceBillPayItem[] payItemArray)
        {
            decimal amount = 0.0M;
            decimal segmentOriginalAmount = 0.0M;
            decimal segmentAcutalAmount = 0.0M;

            if (serviceItemArray != null)
            {
                for (int index = 0; index < serviceItemArray.Length; index++)
                {
                    segmentOriginalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour;
                    segmentAcutalAmount += serviceItemArray[index].Price * serviceItemArray[index].Hour * serviceItemArray[index].Discount;
                }
            }
            amount += segmentAcutalAmount;

            segmentOriginalAmount = 0.0M;
            segmentAcutalAmount = 0.0M;
            if (materialArray != null)
            {
                for (int index = 0; index < materialArray.Length; index++)
                {
                    segmentOriginalAmount += materialArray[index].Price * materialArray[index].Count;
                    segmentAcutalAmount += materialArray[index].Price * materialArray[index].Count * materialArray[index].Discount;
                }
            }
            amount += segmentAcutalAmount;

            segmentOriginalAmount = 0.0M;
            segmentAcutalAmount = 0.0M;
            if (payItemArray != null)
            {
                for (int index = 0; index < payItemArray.Length; index++)
                {
                    if (payItemArray[index].PayItemType == Domain.Constant.PayItemTypes.Discount)
                    {
                        segmentOriginalAmount += payItemArray[index].Amount;
                    }
                    else
                    {
                        segmentAcutalAmount += payItemArray[index].Amount;
                    }
                }
            }
            amount -= segmentOriginalAmount;
            amount += segmentAcutalAmount;

            return amount;
        }
    }
}
